﻿Imports Training.Common

Partial Public Class QuestionAnswerSelect
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' check for session values
        If IsNothing(Session("qid")) Or IsNothing(Session("qnm")) Or IsNothing(Session("qst")) Or IsNothing(Session("qor")) Then
            Response.Redirect("QuizControl.aspx")
        Else
            lblQuiz.Text = Session("qnm") ' quiz name
            lblQuest.Text = GetQuestionTextByQuestionID(CType(Session("qst"), Integer)) ' question name
        End If
    End Sub

    ''' <summary>
    ''' Insert New Question/Answer combination
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AddQuizQuestion()
        Dim qid As Integer = Session("qid")
        Dim qsid As Integer = Session("qst")
        Dim aid As Integer = ddlAnswer.SelectedValue
        Dim ca As Boolean
        Dim aod As Integer = tbOrder.Text
        Dim qod As Integer = Session("qor")

        ' check that answer order is in correct format
        If Not IsNumeric(aod) Then
            lblMsg.Text = "Answer Order MUST be numeric"
            Exit Sub
        End If

        ' detemine correct answer
        If ddlCorrect.SelectedValue = 1 Then
            ca = True
        Else
            ca = False
        End If

        If InsertQuizQAXRef(qid, qsid, aid, ca, aod, qod) <> 0 Then
            lblMsg.Text = "Error inserting Question/Answer. Please try again."
        End If
    End Sub

    ''' <summary>
    ''' Reset form fields to defaults so use can add another answer
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ResetFields()
        ddlAnswer.SelectedValue = "-1"
        tbOrder.Text = String.Empty
        ddlCorrect.SelectedValue = "-1"
    End Sub

    ''' <summary>
    ''' Complete button click event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnComplete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComplete.Click
        Response.Redirect("AddEditQuizQuestions.aspx?qid=" & Session("qid") & "&qnm=" & Session("qnm"))
    End Sub

    ''' <summary>
    ''' Continue button click event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnContinue_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnContinue.Click
        AddQuizQuestion()
        ResetFields()
        dsQuestionAnswers.DataBind()
        gvAnswers.DataBind()
    End Sub
End Class